import sys
import os
import re
from PyQt5.QtCore import Qt, pyqtSignal, QObject

from .NvmGenModel import *
# from ...Platform.codegen.codegen import CodeGenerator
from workspace.Platform.codegen import codegen   # 绝对导入

class NvmGenerater(QObject):
    Mysignal = pyqtSignal(int, int, str)

    def __init__(self):
        super(NvmGenerater, self).__init__()
        self.def_path = ''
        print('NvmGenerater init ...')
        self.nvmgene_op = NvmGeneOpera()

    def NvmPreSetExcelPath(self, path):
        self.def_path = path

    def NvmPreStartGenera(self, excelpath):
        self._NvmPreMkFloder(excelpath)
        self._NvmPreGetExcelData(excelpath)
        self._NvmPreInsertExcelToTable()
        self._NvmPreGeneCode()

    def _NvmPreMkFloder(self, excelpath):
        self.nvmgene_op.NvmModelGeneFloder(excelpath)

    def _NvmPreGetExcelData(self, excelpath):
        self.nvmgene_op.NvmModelOpExcel(excelpath)

    def _NvmPreGeneCode(self):
        '''base on Nvm Task to Gennerate Code'''
        # self.nvmgene_op.NvmModelGeneCode()

        ''' base on rules and template to Generate Code '''
        self._NvmPreGeneRules('Nvm', 'Nvm_rules')

        # gennerate Nvm/inc/Nvm_func_bosch_get_did.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_bosch_get_did.h')
        # gennerate Nvm/src/Nvm_func_bosch_get_did.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_bosch_get_did.c')

        # gennerate Nvm/inc/Nvm_func_bosch_get_did.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_bosch_set_did.h')
        # gennerate Nvm/src/Nvm_func_bosch_set_did.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_bosch_set_did.c')

        # gennerate Nvm/inc/Nvm_func_bosch_get_did_mgr.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_bosch_get_did_mgr.h')
        # gennerate Nvm/src/Nvm_func_bosch_get_did_mgr.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_bosch_get_did_mgr.c')

        # gennerate Nvm/inc/Nvm_func_bosch_get_did_mgr.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_bosch_set_did_mgr.h')
        # gennerate Nvm/src/Nvm_func_bosch_get_did_mgr.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_bosch_set_did_mgr.c')

        # gennerate Nvm/inc/Nvm_data.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_data.h')
        # gennerate Nvm/src/Nvm_data.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_data.c')

        # gennerate Nvm/inc/Nvm_func_set.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_set.h')
        # gennerate Nvm/src/Nvm_func_set.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_set.c')

        # gennerate Nvm/inc/Nvm_func_get.h
        self._NvmPreDidRuleOfGeneCode('inc', 'Nvm_func_get.h')
        # gennerate Nvm/src/Nvm_func_get.c
        self._NvmPreDidRuleOfGeneCode('src', 'Nvm_func_get.c')

        # gennerate Nvm/cfg/Nvm_al_cfg.h
        self._NvmPreDidRuleOfGeneCode('cfg', 'Nvm_al_cfg.h')

        print(" Nvm Generate Code Over ...")

    def _NvmPreInsertExcelToTable(self):
        rows = self.nvmgene_op.NvmModelGetExcelRows()
        # cols = self.nvmgene_op.NvmModelGetExcelCols()
        for row in range (1, rows):
            for col in range (0, 13):
                data = self.nvmgene_op.NvmModelGetExcelData(row, col)
                if (col == 8):
                    data = re.sub(r"\n+", " ", data)
                self.Mysignal.emit(row-1, col, data)

    def _NvmPreTrmplateOfGeneCode(self, outfile_name):
        self.rules = self.code_gen.load_rules()
        template_file_name = outfile_name.replace(".", "_") + ".template"
        template_file_index = self._NvmPreGetTemplateIndex(template_file_name)
        self.code_gen.generator(self.rules, self.code_gen.template_files[template_file_index], outfile_name, 'nvm generate')

    def _NvmPreGeneRules(self, ruel_model_name, ruels_name):
        self.code_gen = codegen.CodeGenerator(ruel_model_name, ruels_name)
        self.nvmgene_op.NvmModelGeneRules(ruel_model_name, ruels_name)

    def _NvmPreDidRuleOfGeneCode(self, folder_name, outfile_name):
        self._NvmPreReadTemplateFiles(folder_name)
        self._NvmPreTrmplateOfGeneCode(outfile_name)

    def _NvmPreGetTemplateIndex(self, template_file_name):
        return self.code_gen.template_files.index(template_file_name)

    def _NvmPreReadTemplateFiles(self, folder_name):
        self.code_gen.read_template(folder_name)

    def NvmPreGetGlobalConfigDict(self):
        return self.nvmgene_op.NvmModelGetGolbalCfgDict()
